package com.wongoing.sys.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wongoing.base.PageResult;
import com.wongoing.sys.model.SysUserRole;
import com.wongoing.sys.model.ext.SysRoleExt;
import com.wongoing.sys.model.ext.SysUserRoleExt;
import com.wongoing.sys.service.SysRoleService;
import com.wongoing.sys.service.SysUserRoleService;
import com.wongoing.sys.service.SysUserService;
import com.wongoing.sys.viewmodel.JsonResult;
import com.wongoing.util.LogService;
import com.wongoing.util.ModelAndViewConstants;
import com.wongoing.util.PageUtil;

@Controller
@RequestMapping("/sys/sysUserRoleAction")
public class SysUserRoleAction extends BaseAction{
	
	private SysUserRoleService service;
	private SysRoleService sysRoleService;
	private SysUserService sysUserService;
	@Autowired
	public void setService(SysUserRoleService service) {
		this.service = service;
	}
	@Autowired
	public void setSysRoleService(SysRoleService sysRoleService) {
		this.sysRoleService = sysRoleService;
	}
	@Autowired
	public void setSysUserService(SysUserService sysUserService) {
		this.sysUserService = sysUserService;
	}
	
	/**
	 * 功能说明：进入列表显示界面
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017年10月15日 下午1:06:43
	 * @param pageSize 每页记录数
	 * @param pageCurrent 当前页索引
	 * @param orderField 排序字段
	 * @param orderDirection 排序方向
	 * @param paramAccount 参数：账户
	 * @param paramUserName 参数：用户名
	 * @param paramPostName 参数：岗位名称
	 * @param paramRoleName 参数：角色名称
	 * @return
	 */
	@RequestMapping(value="/toList")
	public ModelAndView toList(Integer pageSize, Integer pageCurrent, String orderField, String orderDirection,
			String paramAccount,String paramUserName,String paramRoleName){
		ModelAndView mv = null;
		try {
			orderField = "userId";
			PageResult<SysUserRoleExt> pageResult = PageUtil.pageSet(
					this.getClass(), pageSize, pageCurrent, orderField,
					orderDirection);
			pageResult.getParam().put("status", "0");
			//模糊查询判断
			List<Boolean> multiFuzzyQuery = new ArrayList<Boolean>();
			if(paramRoleName!=null && !"".equals(paramRoleName)){
				pageResult.getParam().put("roleName", paramRoleName);
				pageResult.setOrderField("t4.Name");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramRoleName", paramRoleName);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			
			if(paramUserName!=null && !"".equals(paramUserName)){
				pageResult.getParam().put("userName", paramUserName);
				pageResult.setOrderField("t1.Name");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramUserName", paramUserName);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			if(paramAccount != null && !"".equals(paramAccount)){
				pageResult.getParam().put("account", paramAccount);
				pageResult.setOrderField("t1.Account");
				pageResult.setOrderDirection("DESC");
				pageResult.getExt().put("paramAccount", paramAccount);
				multiFuzzyQuery.add(true);
			}else{
				multiFuzzyQuery.add(false);
			}
			if(PageUtil.multiFuzzyQuery(multiFuzzyQuery)){
				pageResult.setOrderField("t1.Id");
				pageResult.setOrderDirection("ASC");
			}
			pageResult = this.service
					.getUserRoleConfigListPageResult(pageResult);
			mv = new ModelAndView();
			mv.addObject(ModelAndViewConstants.PAGE_RESULT, pageResult);
			mv.setViewName(ModelAndViewConstants.USER_ROLE_MAIN_VIEW);
			LogService.getInstance(this).debug("用户角色分配数据列表获取成功："+ModelAndViewConstants.USER_ROLE_MAIN_VIEW);
		} catch (Exception ex) {
			LogService.getInstance(this).error("分页获取用户角色列表失败：" + ex.getMessage(), ex);
			mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);
		}
		return mv;
	}
	
	/**
	 * 功能说明：进入角色列表界面
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017年10月15日 下午1:10:10
	 * @param userId
	 * @param roleId
	 * @param pageSize
	 * @param pageCurrent
	 * @param orderField
	 * @param orderDirection
	 * @param paramName
	 * @param paramDes
	 * @return
	 */
	@RequestMapping(value = "/toRoleList")
	public ModelAndView toRoleList(Integer userId,Integer roleId,Integer pageSize, Integer pageCurrent, String orderField, String orderDirection,String paramName, String paramDes){
		ModelAndView mv = null;
		try {
			mv = new ModelAndView();
			//获取权限分页列表数据
			PageResult<SysRoleExt> pageResult = PageUtil.pageSet(
					this.getClass(), pageSize, pageCurrent, orderField,
					orderDirection);
			pageResult.getParam().put("status", "0");
			//模糊查询判断
			if(paramDes!=null && !"".equals(paramDes)){
				pageResult.getParam().put("description", paramDes);
				pageResult.setOrderField("t1.Description");
				pageResult.setOrderDirection("desc");
				pageResult.getExt().put("paramDes",paramDes);
			}
			if(paramName!=null && !"".equals(paramName)){
				pageResult.getParam().put("name", paramName);
				pageResult.setOrderField("t1.Name");
				pageResult.setOrderDirection("desc");
				pageResult.getExt().put("paramName",paramName);
			}
			pageResult = this.sysRoleService.getRoleListPageResult(pageResult);
			pageResult.getExt().put("userId", userId);
			pageResult.getExt().put("roleId",roleId);
			mv.addObject(ModelAndViewConstants.PAGE_RESULT, pageResult);
			mv.setViewName(ModelAndViewConstants.USER_ROLE_EDIT_VIEW);
			LogService.getInstance(this).debug("用户权限分配编辑列表获取成功："+ModelAndViewConstants.USER_ROLE_EDIT_VIEW);
		} catch (Exception ex) {
			LogService.getInstance(this).error("分页获取用户权限列表失败：" + ex.getMessage(), ex);
			mv = new ModelAndView(ModelAndViewConstants.ERROR_VIEW);
		}
		return mv;
	}
	/**
	 * 功能说明：执行保存操作
	 * 修改说明：
	 * @author zhenglibing
	 * @date 2017年10月15日 下午1:10:45
	 * @param roleId
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/doSave")
	public Map<String, Object> doSave(String roleId,String userId){
		Map<String, Object> jsonResult = null;
		try {
			Map<String,Object> param = new HashMap<String, Object>();
			param.put("userId", userId);
			List<SysUserRole> userRoleList = this.service.getByParam(param);
			Integer statusCode = 200;
			String msg = "设置成功";
			switch (userRoleList.size()) {
			case 0:
				this.service.insert(setEntity(true,roleId,userId,null));
				break;
			case 1:
				this.service.updateByPrimaryKey(setEntity(false,roleId,userId,userRoleList.get(0).getId()));
				break;
			default:
				statusCode = 300;
				msg = "设置失败";
				break;
			}

			String tabid = tabid(ModelAndViewConstants.USER_ROLE_SYS_ID);
			if(statusCode == 200){
				jsonResult = JsonResult.jsonReturn(statusCode.intValue(), msg, tabid);
			}else{
				jsonResult = JsonResult.jsonReturnErr(msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
			String msg = "保存角色信息失败";
			jsonResult = JsonResult.jsonReturnErr(msg);
		}
		return jsonResult;
	}
	
	private SysUserRole setEntity(boolean flag,String roleId,String userId, Integer id){
		Date now = new Date();
		SysUserRole userRole = new SysUserRole();
		if(id!=null){
			userRole.setId(id);
		}
		userRole.setRoleId(Integer.parseInt(roleId));
		userRole.setUserId(Integer.parseInt(userId));
		userRole.setCreateDate(now);
		userRole.setModifyDate(now);
		userRole.setCreateUser(Integer.parseInt(userId));
		userRole.setModifyUser(Integer.parseInt(userId));
		userRole.setStatus("0");
		return userRole;
	}
}
